package com.company.controller;

import com.company.constant.MessageConstant;
import com.company.entity.Result;
import com.company.pojo.OrderSetting;
import com.company.service.CheckItemService;
import com.company.service.OrderSettingService;
import com.company.util.POIUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author fu xiao liu
 * @date 2021-08-10 16:31
 * 预约订单管理（设置预约日期以及人数）
 */

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @DubboReference //查询服务
    private OrderSettingService orderSettingService;

    /**
     * @date  文件上传服务
     * @param file
     * @return com.company.entity.Result
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile file){
        try {
            List<String[]> strings = POIUtils.readExcel(file);
            ArrayList<OrderSetting> arrayList = new ArrayList<>();
            for (String[] string : strings){
                String orderDate =  string[0];
                String number = string[1];
                System.out.println(orderDate + " "+ number);

            }
            for (String[] string : strings){
               String orderDate =  string[0];
               String number = string[1];
                OrderSetting orderSetting = new OrderSetting(new Date(orderDate), Double.valueOf(number).intValue());
                arrayList.add(orderSetting);
            }
            orderSettingService.add(arrayList);
            return new Result( true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        }catch (IOException e){
            e.printStackTrace();
            //文件解析失败
            return new Result( false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        try{
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        }
        catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }




    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try{
            orderSettingService.editNumberByDate(orderSetting);
            //预约设置成功
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            //预约设置失败
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }

    }

}
